package com.ydsungan.controller;


import com.ydsungan.pojo.Book;
import com.ydsungan.pojo.BookExample;
import com.ydsungan.pojo.BookVO;
import com.ydsungan.pojo.User;
import com.ydsungan.service.BookService;
import com.ydsungan.service.CommunityService;
import com.ydsungan.service.FileService;
import com.ydsungan.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import org.json.JSONObject;
import com.ydsungan.common.JSONResult;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(path = "/root")
@Api(value = "管理员相关",tags = {"与管理员相关的接口"})
@CrossOrigin
public class RootController extends BasicController{


    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommunityService communityService;

    @Autowired
    private FileService fileService;

    @ApiOperation(value="基础设置",notes = "动态修改考研日期、练习记录页面天数设置")
    @RequestMapping(value = "/basic", method = RequestMethod.GET)
    public JSONResult saveDate(String date, String days, String showUpload) throws Exception{
        if(date != null){
            redis.set("kaoyandate", date);
        }
        if(days != null){
            redis.set("days", days);
        }
        //0表示不开启，1表示开启，默认不开启
        if(showUpload != null){
            redis.set("showUpload", showUpload);
        }else{
            redis.set("showUpload", "0");
        }

        return JSONResult.ok();
    }

    @ApiOperation(value="列举书本",notes = "为后台管理系统列举书本")
    @RequestMapping(value = "/listbook", method = RequestMethod.GET)
    public JSONResult listBook() throws Exception{
        return JSONResult.ok(bookService.listAllBooks());
    }

    @ApiOperation(value="首页数据一览",notes = "获取首页多项数据")
    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public JSONResult getHomePageData() throws Exception{
        ArrayList<Long> list = new ArrayList<Long>();
        list.add(userService.countUsers());
        list.add(userService.countTodayActiveUsers());
        list.add(communityService.countPosts(false));
        list.add(communityService.countPosts(true));
        list.add(fileService.countFiles());
        list.add(fileService.countNeedCheckFiles());

        return JSONResult.ok(list);
    }




}
